#!/bin/sh
################################################################################
#
# Copyright (C) 2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################
# File: cpuisolation_cpuisolation03.sh
#
# Description: 
#
# Authors:     Ma Feng - mafeng.ma@huawei.com
#
# History:     April 14 2022 - init scripts
#
################################################################################
source tst_oh.sh

do_setup()
{
	for i in $(seq 0 19)
	do
        sh cpu_pro > /dev/null 2>&1 &
	done
}

do_test()
{
    local ret=0
	
	tst_res TINFO "Start test cupisolation_cpuisolation03"
	
	echo 2 > /sys/devices/system/cpu/cpu0/core_ctl/max_cpus
	cat /sys/devices/system/cpu/cpu0/core_ctl/global_state
	
	
	cpu_isolation_info=($(cat /sys/devices/system/cpu/cpu0/core_ctl/global_state | grep 'Isolated' | sed 's/.*Isolated: \([0-9]\)/\1/g'))
	for i in "${!cpu_isolation_info[@]}"; do
	    if [[ "${cpu_isolation_info[$i]}" == "0" ]]; then
			tst_res TINFO "CPU${i} is isolation!"
		fi
	done
	
	cpu_isolation_total=$(cat /sys/devices/system/cpu/cpu0/core_ctl/global_state | grep 'Isolated: 1' | wc -l)
	if [[ "$cpu_isolation_total" == "2" ]]; then
	    tst_res TINFO "Two cpus are isolated, and the remaining two are still active!"
	else
		ret=$(( $ret + 1 ))
	fi
	
	
	echo 0 > /sys/devices/system/cpu/cpu0/online 
	cat /proc/sched_debug
	cat /sys/devices/system/cpu/cpu0/core_ctl/global_state
	
	cpu_task_info=$(cat /proc/sched_debug | grep "cpu0")
	cpu_is_online=($(cat /sys/devices/system/cpu/cpu0/core_ctl/global_state | grep 'Online' | sed 's/.*Online: \([0-9]\)/\1/g'))
	
	if [[ "$cpu_task_info" == "cpu#0" && "$cpu_is_online" == "0" ]] ; then
	    tst_res TINFO "CPU is offline!"
        for i in "${!pid_list[@]}"; do
		    if [[ "$(cat /proc/sched_debug | grep "${pid_list[$i]}")" == "" ]] ; then
			    tst_res TINFO "Task ${pid_list[$i] not move!}"
				ret=$(( $ret + 1 ))
		    fi
		done
	fi
	
	echo 1 > /sys/devices/system/cpu/cpu0/online 
	cat /proc/sched_debug
	cat /sys/devices/system/cpu/cpu0/core_ctl/global_state
	
	cpu_is_online=($(cat /sys/devices/system/cpu/cpu0/core_ctl/global_state | grep 'Online' | sed 's/.*Online: \([0-9]\)/\1/g'))
	if [[ "$cpu_is_online" == "1" ]] ; then
        tst_res TINFO "CPU is online!"
	else
		ret=$(( $ret + 1 ))
    fi
	
	cpu_isolation_total=$(cat /sys/devices/system/cpu/cpu0/core_ctl/global_state | grep 'Isolated: 1' | wc -l)
	if [[ "$cpu_isolation_total" == "2" ]] ; then
	    tst_res TINFO "Two cpus are isolated, and the remaining two are still active!"
	else
		ret=$(( $ret + 1 ))
	fi
	
	echo 0 > /sys/devices/system/cpu/cpu0/online
	echo 0 > /sys/devices/system/cpu/cpu1/online
	echo 0 > /sys/devices/system/cpu/cpu2/online
	cat /proc/sched_debug
	cat /sys/devices/system/cpu/cpu0/core_ctl/global_state
	
	echo 1 > /sys/devices/system/cpu/cpu0/online
	echo 1 > /sys/devices/system/cpu/cpu1/online
	echo 1 > /sys/devices/system/cpu/cpu2/online
	cat /proc/sched_debug
	cat /sys/devices/system/cpu/cpu0/core_ctl/global_state
	
	for i in $(seq 0 3);do
	    cpu_is_online=$(cat /proc/sched_debug | grep 'cpu#$i')
		if [[ "$cpu_is_online" == "" ]] ;then
		    for i in "${!pid_list[@]}"; do
				if [[ "$(cat /proc/sched_debug | grep "${pid_list[$i]}")" == "" ]] ; then
					tst_res TINFO "Task ${pid_list[$i] not move!}"
					ret=$(( $ret + 1 ))
				fi
			done
		else
		    ret=$(( $ret + 1 ))
		fi
	done
	
	cpu_isolation_total=$(cat /sys/devices/system/cpu/cpu0/core_ctl/global_state | grep 'Isolated: 1' | wc -l)
	if [[ "$cpu_isolation_total" == "2" ]] ; then
	    tst_res TINFO "Two cpus are isolated, and the remaining two are still active!"
	else
		ret=$(( $ret + 1 ))
	fi
	
	if [ $ret -eq 0 ];then
		tst_res TPASS "Isolation cpu's task move are implemented pass."
	else
		tst_res TFAIL "isolation cpu's task move are implemented failed!"
	fi
}

do_clean()
{
    killall cpu_pro
}


do_setup
do_test
do_clean
tst_exit
